namespace Core.Features
{
    using System;
    using Base;
    using Contracts.Enums;
    using Resources;
    using System.Linq;
    using Contracts.Interfaces;
    using Extensions;

    
    public sealed class ReloadingTime<T> : FeatureBase<object>
    {
        public ReloadingTime(){}

        public ReloadingTime(T value) 
            : base(value){ }
        /// <summary>
        /// Obtains the feature name
        /// </summary>
        public override string Name
        {
            get { return "Reloading time"; }
        }
        /// <summary>
        /// Obtains the feature group
        /// </summary>
        public override FeatureGroupsEnum FeatureGroup { get { return FeatureGroupsEnum.General; } }
        /// <summary>
        /// Obtains the feature type
        /// </summary>
        public override FeaturesEnum FeatureType { get { return FeaturesEnum.ReloadingTime; } }
        /// <summary>
        /// Obtains or defines the feature unit
        /// </summary>
        public override string Unit { get { return Traduction.label_unit_sec; } }

        public override void SetModifiedValue(IAgent agent, IRobot robot)
        {
            if (agent == null || robot == null) return;
            
            // Bonus from Accelerated Reloading extension
            var accReloadingExtensio = agent.Extensions.Where(e => e.GetType() == typeof(AcceleratedReloading) && e.Modifiers != null && e.Modifiers.Any()).FirstOrDefault();
            if (accReloadingExtensio == null) return;

            double temp = (double) Value/ (1 + accReloadingExtensio.Modifiers.Where(m => m.ModifiedFeatureType == FeatureTypesEnum.Robot && m.Feature == this.FeatureType).First().Value*accReloadingExtensio.Level);
            ModifiedValue = IsRounded
                       ? Math.Round(temp, 0, MidpointRounding.AwayFromZero)
                       : Math.Round(temp, 2);
        }
        
    }
}